﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SuperFox.SuperServiceReference;
using System.Windows.Data;

namespace SuperFox
{
    public partial class ItemOptions : UserControl
    {
        public static readonly DependencyProperty OptionsProperty =
           DependencyProperty.Register("Options", typeof(ObservableCollection<DbProductOptionsValues>), typeof(ItemOptions),
            new PropertyMetadata(new ObservableCollection<DbProductOptionsValues>(), new PropertyChangedCallback(OptionsPropertyChanged)));

        public static readonly DependencyProperty OptionsNameProperty =
           DependencyProperty.Register("OptionsName", typeof(string), typeof(ItemOptions),
            new PropertyMetadata("", new PropertyChangedCallback(OptionsNamePropertyChanged)));

        private ObservableCollection<DbPriceTiers> price;

        public ObservableCollection<DbPriceTiers> Price
        {
            get { return price; }
            set { price = value; }
        }

        public string OptionsName
        {
            get { return (string)GetValue(OptionsNameProperty); }
            set { SetValue(OptionsNameProperty, value); }
        }

        public ObservableCollection<DbProductOptionsValues> Options
        {
            get { return (ObservableCollection<DbProductOptionsValues>)GetValue(OptionsProperty); }
            set { SetValue(OptionsProperty, value); }
        }

        private static void OptionsNamePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ItemOptions).OptionName.Text = e.NewValue as string;
        }

        private static void OptionsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as ItemOptions).ItemsListBox.ItemsSource = e.NewValue as ObservableCollection<DbProductOptionsValues>;

        }


        public ItemOptions()
        {
            InitializeComponent();

            if (!(App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.ShowAvailableStock)
            {
                StockTb.Visibility = System.Windows.Visibility.Collapsed;
            }            

        }

        private void Number_TextChanged(object sender, TextChangedEventArgs e)
        {
            // calculate the totals


            try
            {
                decimal number = Decimal.Parse((sender as TextBox).Text);

                if (number > (int)(sender as TextBox).Tag)
                {
                    (sender as TextBlock).Text = ((int)(sender as TextBlock).Tag).ToString();
                }

                //((sender as TextBox).DataContext as DbProductOptionsValues).TotalSelected = (int)Decimal.Round(number * this.Price, 2);

                //decimal total = 0;

                //decimal total = 0;

                //foreach (DbProductOptionsValues v in this.Options)
                //{
                //    total += v.TotalSelected;
                //}

                //OverAllTotal.Text = Decimal.Round(total * this.Price, 2).ToString();

            }
            catch (Exception)
            { }
        }

        private void Total_Loaded(object sender, RoutedEventArgs e)
        {

            

            Binding b = new Binding()
            {
                Converter = new TotalPriceConverter(),
                Source = (sender as TextBlock).DataContext,
                ConverterParameter = this.Price,
                Mode = BindingMode.TwoWay,
                Path = new PropertyPath("TotalSelected")
            };

            
            (sender as TextBlock).SetBinding(TextBlock.TextProperty, b);
        }

        private void TextBlock_Loaded(object sender, RoutedEventArgs e)
        {
            if (!(App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.ShowAvailableStock)
            {
                (sender as TextBlock).Visibility = System.Windows.Visibility.Collapsed;
            }
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            if (!(App.Current.RootVisual as MainPage).CurrentUser.Programs.Style.ShowAvailableStock)
            {
                (sender as Grid).ColumnDefinitions[1].Width = GridLength.Auto;
                (sender as Grid).ColumnDefinitions[0].Width = new GridLength(120);
            }
        }

        //private void OverAllTotal_Loaded(object sender, RoutedEventArgs e)
        //{
        //    Binding b = new Binding()
        //    {
        //        Converter = new TotalPriceTallyConverter(),
        //        Source = this,
        //        Path = new PropertyPath("Options"),
        //        Mode = BindingMode.OneWay
        //    };

        //    (sender as TextBlock).SetBinding(TextBlock.TextProperty, b);
        //}
    }
}
